<template>
	<view>
		<view class="loadingTxt" v-if="loadFlag">
			<loading></loading>
		</view>
		<template v-else>
			<!-- 咨询师基本信息 -->
			<zxs-info :zxsInfo="zxsInfo" :parScName="parScName"></zxs-info>
			<!-- 咨询师总评分和订单评论 -->
			<view class="totalPj spaceBet" v-if="zxsInfo.pjTotalNum > 0" @tap="goMorePj">
				<view class="leftPart hasFlex">
					<view class="starView">
						<htz-rate v-model="pjScore" :size="36" :gutter="10" :readonly="true" :type="1"></htz-rate>
					</view>
					<view class="scoreNum">{{zxsInfo.pjScore}}<text class="unit">分</text></view>
				</view>
				<view class="rightPart hasFlex">
					<view class="pjNum">{{zxsInfo.pjTotalNum}}人评价</view>
					<view class="iconfont icon-gengduo"></view>
				</view>
			</view>
			<!-- 介绍tab 介绍 评价 解答 @goCurrLocation="goCurrLocation"-->
			<zxs-tab 
				:zxsInfo="zxsInfo" 
				:scRange="scRange" 
				@goMorePj="goMorePj"
				@viewMoreAnsQue="viewMoreAnsQue"
				@previewImage="previewImage"
			></zxs-tab>
			<!-- 底部btn -->
			<view class="botZxBtn">
				<!-- 2024-06-04 新增咨询套餐 -->
				<zx-tc-entry @showMoreZxTc="showMoreZxTc" v-if="hasZxTcFlag" :firstTcName="firstTcName"></zx-tc-entry>
				<zxs-btn :hasZxTcFlag="hasZxTcFlag" :imRefuseNum="imRefuseNum" :gzTag="gzTag" @makePhoneCall="makePhoneCall" @focusZxs="focusZxs" @goYyZxs="goYyZxs" @goZxChart="goZxChart"></zxs-btn>
			</view>
			<common-alert :showAlertFlag="showAlertFlag" :maskClosable="maskClosable" @closeAlertComponent="closeAlertComponent">
				<kf-tel :kfTel="kfTel" :phones="phones" @cancel="cancel" @phoneCall="phoneCall"></kf-tel>
			</common-alert>
			<uni-popup type="bottom" ref="zxTcListPop">
				<zx-tc-list :tcList="tcList" @goZxTcIntro="goZxTcIntro" @goBuyZxTc="goBuyZxTc"></zx-tc-list>
			</uni-popup>
			<uni-popup type="bottom" ref="zxTcIntroPop">
				<zx-tc-profile :tcItem="tcItem" @goBuyZxTc="goBuyZxTc"></zx-tc-profile>
			</uni-popup>
		</template>
	</view>
</template>


<script>
	import ZxsInfo from './components/ZxsInfo'
	import ZxsTab from './components/ZxsTab'
	import ZxTcEntry from './components/ZxTcEntry'
	import ZxTcList from './components/ZxTcList'
	import ZxTcProfile from './components/ZxTcProfile'
	import ZxsBtn from './components/ZxsBtn'
	import jyfParser from "@/components/jyf-parser/jyf-parser";
	import { getZxsInfo,addGzZxs } from '@/api/xlzx/index'
	import zxTcMixsin from './mixins/zxTcMixsin';
	export default {
		components:{
			ZxsInfo,
			ZxsTab,
			ZxTcEntry,
			ZxTcList,
			ZxTcProfile,
			ZxsBtn,
			jyfParser
		},
		mixins:[zxTcMixsin],
		data(){
			return{
				loadFlag:true,
				userId:'',
				zxsInfo:{},
				parScName:[],
				scRange:[],
				albumList:[],//咨询师相册
				gzTag:null,
				mobile:'',
				zxsUserId:'',
				pjScore:0,
				currPage:'',
				setFocFlag:false,
				zxsAcc:'',
				phones:'',
				kfTel:'',
				showAlertFlag:false,
				maskClosable:true,
				timer:null,
				imRefuseNum:-1,//IM是否欠费 0未欠费 1 欠费
				hasZxTcFlag:null,
				tcList:[],
				firstTcName:'',
				tcProfile:'',
				tcItem:{}
			}
		},
		onLoad(options){
			this.userId = options.userId;
			this.phones = 'tel:' + uni.getStorageSync('mpOgzTel');
			this.kfTel = uni.getStorageSync('mpOgzTel');
			this.currPage = options.currPage == undefined ? '' : options.currPage;//zxsFocPage 我的关注咨询师进来
			this.imRefuseNum = uni.getStorageSync('IMRefuseNum') ? uni.getStorageSync('IMRefuseNum') : 0;
			this.loadZxsInfo();
		},
		onUnload(){
			if(this.currPage == 'zxsFocPage' && this.setFocFlag){
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2];
				prevPage.$vm.isRefreshFlag = true;
			}
			uni.removeStorageSync('uoList');
			uni.removeStorageSync('zxTcInfoField');
		},
		methods:{
			goBuyZxTc(tcId){
				uni.showLoading({ title: '下单中...', mask: true });
				let ogzId = uni.getStorageSync('mpOgzId');
				let field = { ogzId,tcId,jfTag:0 }
				this.addTcOrder(field,'','zxTcPay');
			},
			showMoreZxTc(){
				this.$refs.zxTcListPop.open();
			},
			goZxTcIntro(item){
				this.tcItem = item;
				this.$refs.zxTcIntroPop.open();
			},
			onShareAppMessage(res){
				let imageUrl = this.zxsInfo.zjImg.startsWith('http') ? this.zxsInfo.zjImg : this.cloudUrl + this.zxsInfo.zjImg
				return{
					title:this.zxsInfo.realName + '咨询师详情',
					imageUrl
				}
			},
			closeAlertComponent(){
				this.showAlertFlag = false;
			},
			cancel(){
				this.showAlertFlag = false;
			},
			phoneCall(){
				uni.makePhoneCall({
					phoneNumber:this.kfTel
				})
			},
			makePhoneCall(){
				this.showAlertFlag = true;
			},
			goZxChart(){
				if(uni.getStorageSync('userIdPss')){
					let id = "C2C" + this.zxsAcc;
					const url = `../../subIM/TUI-Chat/chat?conversationID=${id}&userAcc=${this.zxsAcc}`;
					if(!uni.getStorageSync('IMRefuseNum')){//未欠费
						if(getApp().globalData.isSDKReady){
							uni.navigateTo({
								url
							});
						}else{//账号登录状态在 即时通信登录状态丢失
							var logAcc = uni.getStorageSync('logAcc');
							uni.showLoading({
								title:'加载中...',
								mask:true
							})
							this.User.__loadIM(logAcc);
							this.timer = setInterval(()=>{
								if(getApp().globalData.isSDKReady){
									uni.navigateTo({
										url,
										success:()=>{
											clearInterval(this.timer);
											uni.hideLoading();
										}
									});
								}
							},1000)
						}
					}
				}else{
					this.util.comNavigateToLogin('userFix');
				}
			},
			previewImage(index){
				var current = this.zxsInfo.upList[index].zlImg;
				uni.previewImage({
					current: current,
					urls: this.albumList
				})
			},
			focusZxs(){
				this.setFocusZxs();
			},
			setFocusZxs(){
				if(uni.getStorageSync('userIdPss')){
					var field = { zxsUserId:this.zxsUserId };
					addGzZxs(field).then(res=>{
						this.gzTag = !this.gzTag;
						if(this.gzTag){
							this.zxsInfo.scNum += 1;
						}else{
							this.zxsInfo.scNum -= 1;
						}
						this.setFocFlag = true;
					}).catch(err=>{
						console.log(err)
					});
				}else{
					this.util.comNavigateToLogin('userFix');
				}
			},
			goYyZxs(){
				if(uni.getStorageSync('userIdPss')){
					uni.navigateTo({
						url:'../zxsYy/zxsYy?zxsUserId=' + this.zxsUserId + '&zxsName=' + this.zxsInfo.realName + '&zxPrice=' + this.zxsInfo.serviceFee
					})
				}else{
					this.util.comNavigateToLogin('userFix');
				}
			},
			loadZxsInfo(){
				var field = { userId:this.userId,lon:'',lat:'' };
				this.loadFlag = true;
				getZxsInfo(field).then(res=>{
					this.loadFlag = false;
					let tmpRes = res.data.datas[0];
					this.zxsInfo = tmpRes;
					this.zxsAcc = tmpRes.mobile;
					this.gzTag = tmpRes.gzTag;
					this.mobile = tmpRes.mobile;
					this.zxsUserId = tmpRes.zxsUserId;
					this.pjScore = tmpRes.pjScore;
					for(var i=0;i<this.zxsInfo.scRange.length;i++){
						this.scRange.push(this.zxsInfo.scRange[i]);
						this.parScName.push(this.zxsInfo.scRange[i].parentLyName);
					}
					if(this.zxsInfo.upList.length > 0){ 
						let tmpArr = this.zxsInfo.upList;
						this.albumList = tmpArr.map(item=>{
							return item.zlImg.startsWith('http') ? item.zlImg : this.cloudUrl + item.zlImg;
						})
					}
					if(this.zxsInfo.uoList.length > 0){
						uni.setStorageSync('uoList',this.zxsInfo.uoList);
					}
					this.tcList = tmpRes.tcList;
					if(this.tcList.length > 0){
						this.hasZxTcFlag = true;
						this.firstTcName = this.tcList[0].tcName;
					}else{
						this.hasZxTcFlag = false;
					}
					setTimeout(()=>{
						uni.setNavigationBarTitle({
							title:this.zxsInfo.realName + '咨询师详情'
						})
					},100);
				}).catch(err=>{
					this.loadFlag = false;
					console.log(err)
				})
			},
			goMorePj(){
				uni.navigateTo({
					url:'../zxsPj/zxsPj?zxsUserId=' + this.zxsUserId
				})
			},
			viewMoreAnsQue(){
				uni.navigateTo({ 
					url:'../zxsJd/zxsJd?zxsUserId=' + this.zxsUserId
				})
			}
		}
	}
</script>
<style lang="scss">
	page{
		background: $pss-bg-primary;
	}
</style>
<style lang='scss' scoped>
	.totalPj{
		padding: 30rpx 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		align-items: center;
		.rightPart{
			font-size: $font-size26;
			color: $pss-text-colora;
			align-items: center;
			.icon-gengduo{
				font-size: $font-size26;
				margin-left: 10rpx;
			}
		}
		.leftPart{
			font-size: 38rpx;
			color: $pss-text-color3;
			font-weight: bold;
			.unit{
				font-weight: normal;
				font-size: 26rpx;
			}
			.scoreNum{
				margin-left: 10rpx;
			}
		}
	}
	.botZxBtn{
		width: 100%;
		position: sticky;
		left: 0;
		bottom: 0;
	}
</style>